package com.alexander.cloundexplorer.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.params.DelFileParams;
import com.alexander.cloundexplorer.bean.params.TrashFileRecoverParams;
import com.alexander.cloundexplorer.bean.response.FileTrashResponse;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 * @author Administrator
 * @date 2018/3/9
 */

public class TrashFileOptionsDialog extends BaseDialog
{
    private FileTrashResponse fileTrashResponse;
    private int layoutId = R.layout.dialog_trashfileoptions_layout;
    private TrashFileRecoverParams trashFileRecoverParams = new TrashFileRecoverParams();
    private DelFileParams delFileParams = new DelFileParams();

    public TrashFileOptionsDialog(@NonNull Context context1, FileTrashResponse fileTrashResponse, User user1)
    {
        super(context1, user1);
        this.fileTrashResponse = fileTrashResponse;
    }

    public TrashFileOptionsDialog(@NonNull Context context1, int themeResId, FileTrashResponse fileTrashResponse, User user1)
    {
        super(context1, themeResId, user1);
        this.user = user1;
        this.fileTrashResponse = fileTrashResponse;
    }

    public TrashFileOptionsDialog(@NonNull Context context1, boolean cancelable, @Nullable OnCancelListener cancelListener, FileTrashResponse fileTrashResponse, User user1)
    {
        super(context1, cancelable, cancelListener, user1);
        this.user = user1;
        this.fileTrashResponse = fileTrashResponse;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(layoutId);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.reCover_TextView, R.id.del_TextView})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.reCover_TextView:
                trashFileRecoverParams.setAccount(user.getPhone());
                trashFileRecoverParams.setPassword(user.getPassword());
                trashFileRecoverParams.setFileId(fileTrashResponse.getFile_id());
                trashFileRecoverParams.setFileName(fileTrashResponse.getFileTrashName());
                String json = JSON.toJSONString(trashFileRecoverParams);
                OkHttpUtils
                        .post()
                        .url(Constants.IP.concat("/fileController/trashFileRecover"))
                        .addParams("params", json)
                        .build()
                        .execute(new StringCallback()
                        {
                            @Override
                            public void onError(Call call, Exception e, int id)
                            {}
                            @Override
                            public void onResponse(String response, int id)
                            {
                                showTip(response);
                                if(response.contains("1400"))
                                {}
                                else if(response.contains("1401"))
                                {}
                            }
                            @Override
                            public void onAfter(int id)
                            {
                                super.onAfter(id);
                                TrashFileOptionsDialog.this.dismiss();
                            }
                        });
                dismiss();
                break;
            case R.id.del_TextView:
                new AlertDialog.Builder(getContext())
                        .setMessage("请确认删除文件？(不可恢复)")
                        .setNegativeButton("取消", new OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确认", new OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                dialog.dismiss();
                                delFileParams.setAccount(user.getPhone());
                                delFileParams.setPassword(user.getPassword());
                                delFileParams.setFileId(String.valueOf(fileTrashResponse.getFile_id()));
                                OkHttpUtils
                                        .post()
                                        .url(Constants.IP.concat("/fileController/delFile"))
                                        .addParams("params", JSON.toJSONString(delFileParams))
                                        .build()
                                        .execute(new StringCallback()
                                        {
                                            @Override
                                            public void onError(Call call, Exception e, int id)
                                            {}

                                            @Override
                                            public void onResponse(String response, int id)
                                            {
                                                showTip(response);
                                            }
                                            @Override
                                            public void onAfter(int id)
                                            {
                                                super.onAfter(id);
                                                TrashFileOptionsDialog.this.dismiss();
                                            }
                                        });
                            }
                        })
                        .show();
                break;
            default:
                break;
        }
    }
}
